<?php
/**
 * 订单商品表
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/22
 * Time: 14:11
 */

namespace Resource\Service;
use Think\Model;

class OrderItemService extends Model
{
    protected $trueTableName = 'wz_order_item';
    /**
     * @param $params
     * @return array|bool
     */
    public function addOrderItem($params) {
        $params['create_time'] = time();
        $params['update_time'] = time();
        $id = D('Resource/OrderItem')->addOrderItem($params);
        if($id) {
            return $id;
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getOrderItemInfo($params, $fields='') {
        $dataInfo = D('Resource/OrderItem')->getOrderItemInfo($params,$fields);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchOrderItemList($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        $data['status'] = ['in',[-1,0,1]];
        isset($params['status']) && $data['status'] =$params['status'];

        //查询总数
        $total = D('Resource/OrderItem')->getOrderItemCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询列表
        $dataList = D('Resource/OrderItem')->getOrderItemList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['dataList'] = $dataList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateOrderItemInfo($params) {
        return D('Resource/OrderItem')->updateOrderItemInfo($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getAllOrderItemList($params){
        $dataInfo = D('Resource/OrderItem')->getAllOrderItemList($params);
        if(is_array($dataInfo) && count($dataInfo)){
            foreach ($dataInfo as $k=>$data){
                //订单信息
                $orderInfo = D('Resource/Order','Service')->getOrderInfo(['id'=>$data['order_id']]);
                //客户信息
                $customer = D('Resource/Customer','Service')->getCustomerInfo(['id'=>$orderInfo['customer_id']]);
                //一级
                $first = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$customer['cid']]);
                //二级
                $second = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$data['second_cid']]);
                //三级
                $three = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$data['three_cid']]);
                //产品
                $item = D('Resource/Item','Service')->getItemInfo(['id'=>$data['item_id']]);
                //模板
                $dataInfo[$k]['tpl'] = D('Resource/Enquiry','Service')->getEnquiryInfo(['id'=>$data['eid']]);
                //模板详情
                $dataInfo[$k]['tpl']['tpl_detail'] = D('Resource/EnquiryDetail','Service')->getAllEnquiryDetail(['eid'=>$data['eid']]);
                $dataInfo[$k]['first_name'] = $first['name'];
                $dataInfo[$k]['second_name'] = $second['name'];
                $dataInfo[$k]['three_name'] = $three['name'];
                $dataInfo[$k]['item_name'] = $item['name'];
                $dataInfo[$k]['item_put_port'] = explode(',',$item['put_port']);
                $dataInfo[$k]['item_put_place'] = explode(',',$item['put_place']);
            }
        }
        return $dataInfo;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function deleteOrderItem($params){
        return D('Resource/OrderItem')->deleteOrderItem($params);
    }

    /**
     * @param $params
     * @return int
     */
    public function getOrderItemCount($params){
        $total = D('Resource/OrderItem')->getOrderItemCount($params);
        return intval($total);
    }
}